package com.wondertek.poms.core.service.impl;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.core.service.IPcProductRuleService;
import com.wondertek.poms.dao.po.PcProductRule;
import com.wondertek.poms.dao.repo.IPcProductRuleDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p><b>PcProductRuleServiceImpl Description:</b> 二级产品规则 service层实现 </p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Service
public class PcProductRuleServiceImpl extends ServiceImpl<PcProductRule, Long> implements IPcProductRuleService {


	@Autowired
	private IPcProductRuleDao pcProductRuleDao;

	/**
	 * 逻辑删除二级产品规则
	 * @param id 需要删除的id
	 * @return 删除的对象
	 */
	@Override
	@Transactional
	public PcProductRule deleteLogic(Long id) {
		PcProductRule cp = jpaRepository.getOne(id);
		PcProductRule.putTimeAndUser(cp);
		cp.setDeleted(Constants.DELETE_1);
		jpaRepository.save(cp);
		return cp;
	}

	/**
	 * 更新二级产品规则
	 * @param model 需更新的规则
	 * @return 更新后的规则
	 */
	@Override
	@Transactional
	public PcProductRule merge(PcProductRule model) {
		PcProductRule.putTimeAndUser(model);
		PcProductRule dbo = jpaRepository.getOne(model.getId());
		CopyUtils.copyProperties(model, dbo);
		return jpaRepository.save(dbo);
	}

	/**
	 * 更新二级产品id查询规则
	 * @param secondId  二级产品id
 	 * @return 规则列表
	 */
	@Override
	public List<PcProductRule> findRuleBySecondId(Long secondId) {
		PcProductRule pr = new PcProductRule();
		pr.setProductInfoPackageId(secondId);
		List<PcProductRule> prs = pcProductRuleDao.findAll(Example.of(pr));
		return prs;
	}

	/**
	 * 更新二级产品规则
	 * @param pcProductRule 更新二级产品规则
	 * @return 更新后的二级产品规则
	 */
	@Override
	public PcProductRule update(PcProductRule pcProductRule) {
		PcProductRule.putTimeAndUser(pcProductRule);
		PcProductRule dbo = jpaRepository.getOne(pcProductRule.getId());
		BeanUtils.copyProperties(pcProductRule, dbo);
		return jpaRepository.save(dbo);
	}

	/**
	 * 分页查询二级产品规则
	 * @param entity 查询规则vo
	 * @param page 页码
	 * @param rows 条数
	 * @param sord 排序列
	 * @param sidx 排序方式
	 * @return 分页列表
	 */
	public Page<PcProductRule> findAll(PcProductRule entity, int page, int rows, String sord, String sidx) {
		Pageable pageable = getPageable(page - 1, rows, sidx, sord);
		Specification<PcProductRule> specification = getWhereClause(entity);
		return pcProductRuleDao.findAll(specification, pageable);
	}

	/**
	 * 构造查询条件
	 * @param pcProductRule 二级产品规则
	 * @return 查询条件
	 */
	private Specification<PcProductRule> getWhereClause(final PcProductRule pcProductRule) {
		return (Specification<PcProductRule>) (root, query, cb) -> {
			List<Predicate> predicate = new ArrayList<>();
			if (null != (pcProductRule.getProductInfoPackageId())) {
				predicate.add(cb.equal(root.get("productInfoPackageId"), pcProductRule.getProductInfoPackageId()));
			}
			predicate.add(cb.equal(root.get("deleted"), "0"));
			Predicate[] pre = new Predicate[predicate.size()];
			return query.where(predicate.toArray(pre)).getRestriction();
		};
	}

}